package com.hsmw.api.controller.pub;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hsmw.api.vo.DriverLicenceReqVO;
import com.hsmw.api.vo.PlateInfo;
import com.hsmw.api.vo.VehicleLicenceReqVO;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.utils.StringUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;

public class VehicleLicenseCertOCR
{

    public static DataResponse ocr(VehicleLicenceReqVO vo) {
        try{
            Credential cred = new Credential(CommonConstants.SECRET_ID, CommonConstants.SECRET_KEY);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            client.setRegion("ap-beijing");

            VehicleLicenseOCRRequest req = new VehicleLicenseOCRRequest();
            if(CommonConstants.BACK.equals(vo.getCarSide())){
                req.setImageUrl(vo.getBackImageUrl());
                req.setCardSide(vo.getCarSide());
                req.setImageUrl(vo.getBackImageUrl());
                req.setCardSide(vo.getCarSide());
                VehicleLicenseOCRResponse resp = client.VehicleLicenseOCR(req);
                System.out.println(VehicleRegCertOCRResponse.toJsonString(resp));
                PlateInfo plateInfo= new PlateInfo();
                TextVehicleBack back = resp.getBackInfo();
                plateInfo.setPlateNo(back.getPlateNo());
                return new DataResponse(plateInfo);
            }else {
                req.setImageUrl(vo.getFrontImageUrl());
                req.setCardSide(vo.getCarSide());
                VehicleLicenseOCRResponse resp = client.VehicleLicenseOCR(req);
                System.out.println(VehicleRegCertOCRResponse.toJsonString(resp));
                PlateInfo plateInfo= new PlateInfo();
                TextVehicleFront front = resp.getFrontInfo();
                plateInfo.setPlateNo(front.getPlateNo());
                plateInfo.setModel(front.getModel());
                plateInfo.setVehicleType(front.getVehicleType());
                plateInfo.setOwner(front.getOwner());
                return new DataResponse(plateInfo);
            }


         //   req.setCardSide();

        } catch (TencentCloudSDKException e) {
            PlateInfo plateInfo= new PlateInfo();
            String  msg = e.getMessage();
            if(StringUtils.isNotBlank(msg)){
                msg = msg.substring(msg.indexOf("-")+1);
            }
            plateInfo.setMessage(msg);
            return  new DataResponse(CommonConstants.ResponseStatus.OCR_FAIL,plateInfo);
        }
    }


    public static DataResponse driverLicenceocr(DriverLicenceReqVO vo) {
        try{
            Credential cred = new Credential(CommonConstants.SECRET_ID, CommonConstants.SECRET_KEY);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            client.setRegion("ap-beijing");

            DriverLicenseOCRRequest req = new DriverLicenseOCRRequest();
            if(CommonConstants.BACK.equals(vo.getCarSide())){
                req.setImageUrl(vo.getBackImageUrl());
                req.setCardSide(vo.getCarSide());
                req.setImageUrl(vo.getBackImageUrl());
                req.setCardSide(vo.getCarSide());
                DriverLicenseOCRResponse resp = client.DriverLicenseOCR(req);
                System.out.println(VehicleRegCertOCRResponse.toJsonString(resp));
                Object  plateInfo= new JSONObject();
                plateInfo= JSON.toJSON(resp);
                return new DataResponse(plateInfo);
            }else {
                req.setImageUrl(vo.getFrontImageUrl());
                req.setCardSide(vo.getCarSide());
                DriverLicenseOCRResponse resp = client.DriverLicenseOCR(req);
                System.out.println(VehicleRegCertOCRResponse.toJsonString(resp));
                Object  plateInfo= new JSONObject();
                plateInfo= JSON.toJSON(resp);
                return new DataResponse(plateInfo);
            }


            //   req.setCardSide();

        } catch (TencentCloudSDKException e) {
            PlateInfo plateInfo= new PlateInfo();
            String  msg = e.getMessage();
            if(StringUtils.isNotBlank(msg)){
                msg = msg.substring(msg.indexOf("-")+1);
            }
            plateInfo.setMessage(msg);
            return  new DataResponse(CommonConstants.ResponseStatus.OCR_FAIL,plateInfo);
        }
    }

    public static void main(String[] args) {
        DriverLicenceReqVO vo = new DriverLicenceReqVO();
        vo.setFrontImageUrl("https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/124555868381623760965339.jpg");
        vo.setCarSide("FRONT");
        DataResponse resp=  driverLicenceocr(vo);
        resp.toString();
        PlateInfo info  = new PlateInfo();
        System.out.println(resp.toString());

    }

}
